import { Controller, Get, Post, Body, Patch, Param, Delete, Request, Req, Query, Headers, HttpCode, Res, Session } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import * as svgCaptcha from "svg-captcha";


@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {

  }

  @Get()
  findAll(@Query() query) {
    console.log(query);

    return {
      code: 200,
      message: query.name
    }
  }


  @Post()
  create(@Request() req) {

    console.log(req.body);

    return {
      code: 200,
      message: req.body.name
    }
  }

  // @Get(":id")
  // @HttpCode(500)
  // findId(@Param('id') id, @Headers() headers) {
  //   console.log('params:', id);
  //   console.log('headers:', headers);

  //   return {
  //     code: 200
  //   }
  // }

  @Get('code')
  createCode(@Req() req, @Res() res, @Session() session) {
    const Captcha = svgCaptcha.create({
      size: 4,
      fontSize: 50,
      width: 100,
      height: 40,
      background: '#cc9966'
    });

    console.log('code=======>', Captcha);
    session.code = Captcha.text;
    res.type('svg');
    res.status(200).send(Captcha.data);
  }

  @Post('create')
  createUser(@Body() Body, @Session() session) {
    if (session.code.toLowerCase() === Body?.code?.toLowerCase()) {
      return {
        code: 200,
        message: '验证成功'
      }
    } else {
      return {
        code: 200,
        message: '验证失败'
      }
    }
  }
}



